package org.csu.jpetstore.web.servlet;

import org.csu.jpetstore.domain.Account;
import org.csu.jpetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

public class SignoutServlet extends HttpServlet {

    private LogService logService;

    private static final String MAIN_FORM = "/WEB-INF/jsp/catalog/Main.jsp";

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        logService = new LogService();
        HttpSession session = request.getSession();


        Account user = (Account) session.getAttribute("loginAccount");
        Date date = new Date();
        logService.insert(user.getUsername(), date.toString(), "登出");

        session.setAttribute("loginAccount", null);
        request.getRequestDispatcher(MAIN_FORM).forward(request,response);
    }

}
